package easy;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2019/2/27 14:09
 */
public class ImplementStr {
    public static void main(String[] args) {
        Solution solution = new ImplementStr().new Solution();
        String haystack = "mississippi";
        String needle = "issip";

        System.out.println(solution.strStr(haystack, needle));
    }

    private class Solution {
        public int strStr(String haystack, String needle) {
            if (haystack == null || needle == null) {
                return -1;
            }

            if (haystack.trim().isEmpty() && needle.trim().isEmpty()) {
                return 0;
            }

            char[] orignal = haystack.toCharArray();
            char[] matcher = needle.toCharArray();

            if (matcher.length > orignal.length) {
                return -1;
            }


            for (int i = 0; i < orignal.length - matcher.length + 1; i++) {
                boolean flag = true;
                for (int j = 0; j < matcher.length; j++) {
                    if (orignal[i + j] != matcher[j]) {
                        flag = false;
                        break;
                    }
                }
                if (flag) {
                    return i;
                }
            }

            return -1;
        }
    }
}
